﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using Fdd.Aspen.Client.ViewModels.DataChannels;
using System.Diagnostics.Contracts;
using System.ComponentModel.Composition;
using Fdd.Aspen.Services.Web.PresentationModel;
using Fdd.Aspen.Client.ViewModel.Infrastructure;
using System.ServiceModel.DomainServices.Client;


namespace Fdd.Aspen.Client.ViewModels
{
    [ExportViewModel("ListMemberViewModel")]
    public class ListMemberViewModel : BaseViewModel, IListMemberViewModel
    {
        
        private IListMemberViewModelDataChannel _listMemberViewService;

        private MemberPM _selectedMember = new MemberPM();

        private ICommand _saveCommand;
        private ICommand _addNewMemberCommand;
        private ICommand _removeMemberCommand;

        
        [ImportingConstructor]
        public ListMemberViewModel(IListMemberViewModelDataChannel listMemberViewService)
        {
            Contract.Requires(listMemberViewService != null);
            Contract.Ensures(_listMemberViewService != null);

            this._listMemberViewService = listMemberViewService;

            base.IsBusy = true;
            
            this._listMemberViewService.GetAllMembersAsync(GetAllMembersCompleted);
        }


        public IEnumerable<MemberPM> Members
        {
            get { return _listMemberViewService.LoadedMembers; }
        }


        public MemberPM SelectedMember
        {
            get { return _selectedMember; }
            set
            {
                _selectedMember = value;
                NotifyPropertyChanged("SelectedMember");
            }
        }


        public ICommand RemoveCommand
        {
            get { return GetRemoveMemberCommand(); }
        }


        public ICommand SaveCommand
        {
            get { return GetSaveCommand(); }
        }


        public ICommand AddNewMemberCommand
        {
            get { return GetAddNewMemberCommand(); }
        }


        private ICommand GetRemoveMemberCommand()
        {
            if (_removeMemberCommand == null)
                _removeMemberCommand = new RelayCommand(p =>
                                            {
                                                _listMemberViewService.RemoveMember(SelectedMember);
                                                SelectedMember = new MemberPM();
                                            },
                                            p => { return !ShowError; }
                                            );

            return _removeMemberCommand;
        }


        private ICommand GetAddNewMemberCommand()
        {
            if (_addNewMemberCommand == null)
                _addNewMemberCommand = new RelayCommand(p =>
                                            {
                                                _listMemberViewService.AddMember(SelectedMember);
                                                SelectedMember = new MemberPM();
                                            },
                                            p => { return !ShowError; }
                                            );

            return _addNewMemberCommand;
        }

        
        private ICommand GetSaveCommand()
        {
            if (_saveCommand == null)
                _saveCommand = new RelayCommand(p =>
                                    {
                                        base.IsBusy = true;
                                        _listMemberViewService.Save(SaveCompleted);
                                    },
                                    p => { return !ShowError; }
                                    );

            return _saveCommand;
        }


        private void GetAllMembersCompleted(DataChannelLoadOperation<IEnumerable<MemberPM>> result)
        {
            if (result.HasError)
                ShowException(result.Error);

            base.IsBusy = false;
        }


        private void SaveCompleted(DataChannelSaveOperation saveOperation)
        {
            if (saveOperation.HasError)
                ShowException(saveOperation.Error);

            base.IsBusy = false;
        }
    }
}
